package com.legsem.legstar.jca;

import javax.resource.ResourceException;
import javax.resource.spi.LocalTransaction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.legstar.messaging.RequestException;

/**
 * This is a Container managed type transaction.
 * <p/>
 * No need to notify Container Manager because it initiates the methods itself.
 * 
 */
public class LegStarManagedLocalTransactionImpl implements LocalTransaction {

    private LegStarManagedConnection managedConnection;

    /** Logger. */
    private final Log log = LogFactory.getLog(getClass());

    public LegStarManagedLocalTransactionImpl(
            LegStarManagedConnection managedConnection) {
        this.managedConnection = managedConnection;
    }

    public void begin() throws ResourceException {
    }

    public void commit() throws ResourceException {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Transaction commit requested");
            }
            managedConnection.getHostConnection().commitUOW();
        } catch (RequestException e) {
            throw new ResourceException(e);
        }
    }

    public void rollback() throws ResourceException {
        try {
            if (log.isDebugEnabled()) {
                log.debug("Transaction rollback requested");
            }
            managedConnection.getHostConnection().rollbackUOW();
        } catch (RequestException e) {
            throw new ResourceException(e);
        }
    }

}
